package com.hyjx.framework.component.interfaceservice.datauserservice;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hyjx.framework.service.SessionManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hyjx.framework.common.CommonUtil;
import com.hyjx.framework.common.tree.TreeObject;
import com.hyjx.framework.common.tree.TreeUtil;
import com.hyjx.framework.component.codetable.CodeTableManager;
import com.hyjx.framework.component.interfaceservice.dataservice.DataServiceModel;
import com.hyjx.framework.component.interfaceservice.datauser.DataUserModel;
import com.hyjx.framework.orm.dao.Pager;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 数据服务服务--控制器
 */
@Controller
@RequestMapping("/userservice")
public class DataUserServiceController {
	@Resource
	private DataUserSService service;
	
	
	@RequestMapping(value = "/gotoList" , method = RequestMethod.GET)
	public String gotoList1(HttpServletRequest request){
		return "/framework/interfaceservice/datauserservice/datauserservice_list";
	}
	
	
	
	@ResponseBody
	@RequestMapping(value="/gotoAllListTree",produces = "application/json; charset=utf-8")
	public String gotoAllListTree(HttpServletRequest request){
		
		int temp=0;
		
		List<DataUserModel> list=service.queryALLUser();
		
		
		List<TreeObject> treeList=new ArrayList<TreeObject>();
		TreeObject parentnode=new TreeObject();
		parentnode.setId("root");
		parentnode.setText("数据用户");
		parentnode.setAttributes("127f6ee40cc0bd36e050007f01006aa9");
		parentnode.setChecked(false);
		parentnode.setCode("root");
		parentnode.setParent_id("-1");
		parentnode.setSelected(false);
		parentnode.setState("open");
		
		treeList.add(parentnode);
		for(DataUserModel dm:list){
			temp++;
				TreeObject to=new TreeObject();
				to.setAttributes("127f6ee40cc0bd36e050007f01006aa9");
				to.setCode(dm.getUserid());
				to.setChecked(false);
				
				to.setSelected(to.isChecked());
			
				to.setParent_id(parentnode.getId());
				to.setId(dm.getUserid());
				
				to.setText(dm.getUsername());
				to.setUrl("");
				to.setState("closed");
				
				treeList.add(to);
		}
		temp=0;
		String jsonString=TreeUtil.getEasyuiJsonStr(treeList, "-1", "");
	//	request.setAttribute("listtree", list);
		return jsonString;
	//	return "[{\"attributes\":\"127f6ee40cc0bd36e050007f01006aa9\",\"checked\":false,\"children\":[{\"attributes\":\"127f6ee40cc3bd36e050007f01006aa9\",\"checked\":false,\"children\":[{\"attributes\":\"127f6ee40cc4bd36e050007f01006aa9\",\"checked\":false,\"children\":[{\"attributes\":\"127f6ee40cc6bd36e050007f01006aa9\",\"checked\":false,\"children\":[],\"code\":\"303002\",\"endflag\":\"\",\"id\":\"303002\",\"parent_id\":\"3030\",\"selected\":false,\"state\":\"open\",\"text\":\"办公室\",\"url\":\"\"}],\"code\":\"3030\",\"endflag\":\"\",\"id\":\"3030\",\"parent_id\":\"30\",\"selected\":false,\"state\":\"closed\",\"text\":\"医疗器械技术审评中心\",\"url\":\"\"}],\"code\":\"30\",\"endflag\":\"\",\"id\":\"30\",\"parent_id\":\"root\",\"selected\":false,\"state\":\"open\",\"text\":\"直属单位\",\"url\":\"\"}],\"code\":\"root\",\"endflag\":\"\",\"id\":\"root\",\"parent_id\":\"-1\",\"selected\":false,\"state\":\"open\",\"text\":\"北京市食品药品监督管理局\",\"url\":\"\"}]";
	}
	
	
	
	
	@ResponseBody
	@RequestMapping(value="/gotoListTree",produces = "application/json; charset=utf-8")
	public String gotoListTree(HttpServletRequest request,String usertype){
		
		int temp=0;
		
		List<DataUserModel> list=service.queryLimitALLUser(usertype);
		
		
		List<TreeObject> treeList=new ArrayList<TreeObject>();
		TreeObject parentnode=new TreeObject();
		parentnode.setId("root");
		parentnode.setText("数据用户");
		parentnode.setAttributes("127f6ee40cc0bd36e050007f01006aa9");
		parentnode.setChecked(false);
		parentnode.setCode("root");
		parentnode.setParent_id("-1");
		parentnode.setSelected(false);
		parentnode.setState("open");
		
		treeList.add(parentnode);
		for(DataUserModel dm:list){
			temp++;
				TreeObject to=new TreeObject();
				to.setAttributes("127f6ee40cc0bd36e050007f01006aa9");
				to.setCode(dm.getUserid());
				to.setChecked(false);
				
				to.setSelected(to.isChecked());
			
				to.setParent_id(parentnode.getId());
				to.setId(dm.getUserid());
				
				to.setText(dm.getUsername());
				to.setUrl("");
				to.setState("closed");
				
				treeList.add(to);
		}
		temp=0;
		String jsonString=TreeUtil.getEasyuiJsonStr(treeList, "-1", "");
	//	request.setAttribute("listtree", list);
		return jsonString;
	//	return "[{\"attributes\":\"127f6ee40cc0bd36e050007f01006aa9\",\"checked\":false,\"children\":[{\"attributes\":\"127f6ee40cc3bd36e050007f01006aa9\",\"checked\":false,\"children\":[{\"attributes\":\"127f6ee40cc4bd36e050007f01006aa9\",\"checked\":false,\"children\":[{\"attributes\":\"127f6ee40cc6bd36e050007f01006aa9\",\"checked\":false,\"children\":[],\"code\":\"303002\",\"endflag\":\"\",\"id\":\"303002\",\"parent_id\":\"3030\",\"selected\":false,\"state\":\"open\",\"text\":\"办公室\",\"url\":\"\"}],\"code\":\"3030\",\"endflag\":\"\",\"id\":\"3030\",\"parent_id\":\"30\",\"selected\":false,\"state\":\"closed\",\"text\":\"医疗器械技术审评中心\",\"url\":\"\"}],\"code\":\"30\",\"endflag\":\"\",\"id\":\"30\",\"parent_id\":\"root\",\"selected\":false,\"state\":\"open\",\"text\":\"直属单位\",\"url\":\"\"}],\"code\":\"root\",\"endflag\":\"\",\"id\":\"root\",\"parent_id\":\"-1\",\"selected\":false,\"state\":\"open\",\"text\":\"北京市食品药品监督管理局\",\"url\":\"\"}]";
	}
	
	@RequestMapping(value = "/gotoListAdmit", method = RequestMethod.POST)
	@ResponseBody
	public String gotoList(HttpServletRequest request){
		String userid=SessionManager.getString("userid");
		
		Pager pager=service.queryListA(userid);
		for(int i=0;i<pager.getResultList().size();i++){
			DataUserServiceModel temp=(DataUserServiceModel) pager.getResultList().get(i);
			String codesrvname=CodeTableManager.getNameByCode("interface_srv_type", temp.getSrvtype());
			temp.setSrvtype(codesrvname);
		}
		
		/*ServiceConditionModel model=new ServiceConditionModel();
		model.setUserid(userid);
		List li= service.getPageList(model);
		for(int i=0;i<li.size();i++){
			DataServiceModel temp=(DataServiceModel) li.get(i);
			String codesrvname=CodeTableManager.getNameByCode("interface_srv_type", temp.getSrvtype());
			temp.setSrvtype(codesrvname);
		}
		StringBuffer sb = new StringBuffer();
		sb.append("{");
		sb.append("\"total\": "+li.size()+",");
		sb.append("\"rows\": ");
		sb.append(CommonUtil.getJsonString(li).toString());
		sb.append("}");*/
		

		return CommonUtil.getPageJsonStr(pager);
	}
	
	@RequestMapping(value = "/gotoListAdmittemp")
	public String gotoListTemp(HttpServletRequest request,String userid){
		SessionManager.set("userid", userid);
		
	//	return "/framework/interfaceservice/datauser/datauser_list";
		return "/framework/interfaceservice/datauserservice/admitinfo";
	}
	
	
	
	//删除权限信息
	@RequestMapping(value = "/gotoDeleteAdmit", method = RequestMethod.GET)
	@ResponseBody
	public String gotoDeleteAdmit(HttpServletRequest request) {
		String id = request.getParameter("ids");
		
		
		
		if(service.delAdmit(id)>0){
			return "success";
		}else{
			return "failure";
		}
	}
	
	
	//修改status信息
	@RequestMapping(value = "/changeStatus", method = RequestMethod.GET)
	@ResponseBody
	public String changeStatus(HttpServletRequest request) {
		String id = request.getParameter("ids");
		String status=request.getParameter("status");
		if(status==null||status.equals("")){
			status="1";
		}else if(status.equals("0")){
			status="1";
		}else if(status.equals("1")){
			status="0";
		}
		
		DataUserServiceModel model=new DataUserServiceModel();
		model.setId(id);
		model.setStatus(status);
		
		if(service.updateStatus(model)>0){
			return "success";
		}else{
			return "failure";
		}
	}
	
	
	
	@RequestMapping(value = "/gotoAdd",  method = {RequestMethod.GET,RequestMethod.POST})
	public String gotoAdd(HttpServletRequest request) {
		return "/framework/interfaceservice/datauserservice/datauserservice_add";
	}
	
	
	
	
	@RequestMapping(value = "/showService", method = RequestMethod.POST)
	@ResponseBody
	public String showService(HttpServletRequest request,ServiceConditionModel model){
		String userid=SessionManager.getString("userid");
		model.setUserid(userid);
		String list=service.queryService(model);
		
		Pager li=service.getPageList(model);
		
		
		//Pager li= service.getPageList(model);
		for(int i=0;i<li.getResultList().size();i++){
			DataServiceModel temp=(DataServiceModel) li.getResultList().get(i);
			String codesrvname=CodeTableManager.getNameByCode("interface_srv_type", temp.getSrvtype());
			temp.setSrvtype(codesrvname);
		}
		/*StringBuffer sb = new StringBuffer();
		sb.append("{");
		sb.append("\"total\": "+li.size()+",");
		sb.append("\"rows\": ");
		sb.append(CommonUtil.getJsonString(li).toString());
		sb.append("}");*/
		return CommonUtil.getPageJsonStr(li);
	}
	
	
	//批量增加用户权限
	@RequestMapping(value = "/doAdd", method = RequestMethod.POST)
	@ResponseBody
	public String doAddUserService() {
		
		return null;
	}
	
	
	
	
	//单个增加用户服务权限
	@RequestMapping(value = "/doAddSingle",method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String doAddUserServiceSingle(DataUserServiceModel model) {
	//	System.out.println("@@@");
		boolean rs=service.addObject(model);
		
		if(rs){
			return "success";
		}else{
			return "failure";
		}
	}
	
	
	
	
	//向用户展示接口信息中转页面
	
	@RequestMapping(value = "/gotoUserService",  method = {RequestMethod.GET,RequestMethod.POST})
	public String gotoUserService(HttpServletRequest request) {
		String showservicetouser=request.getParameter("userlogname");
		SessionManager.set("showservicetouser", showservicetouser);
		return "/framework/interfaceservice/touser/servicetouser";
	}
	
	//向用户展示接口的信息
	
	@RequestMapping(value = "/doUserService",method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public String doUserService(HttpServletRequest request) {

		
		String showservicetouser=SessionManager.getString("showservicetouser");
		//根据用户登录名找出用户id
		String userid=service.queryUserIdByLogName(showservicetouser);
		SessionManager.set("useridshowservice", userid);
	//	String li=service.queryList(userid);
		Pager pager=service.queryListA(userid);
		for(int i=0;i<pager.getResultList().size();i++){
			DataUserServiceModel temp=(DataUserServiceModel) pager.getResultList().get(i);
			String codesrvname=CodeTableManager.getNameByCode("interface_srv_type", temp.getSrvtype());
			temp.setSrvtype(codesrvname);
		}
		
		/*ServiceConditionModel model=new ServiceConditionModel();
		model.setUserid(userid);
		List li= service.getPageList(model);
		for(int i=0;i<li.size();i++){
			DataServiceModel temp=(DataServiceModel) li.get(i);
			String codesrvname=CodeTableManager.getNameByCode("interface_srv_type", temp.getSrvtype());
			temp.setSrvtype(codesrvname);
		}
		StringBuffer sb = new StringBuffer();
		sb.append("{");
		sb.append("\"total\": "+li.size()+",");
		sb.append("\"rows\": ");
		sb.append(CommonUtil.getJsonString(li).toString());
		sb.append("}");*/
		
		//System.out.println(CommonUtil.getPageJsonStr(pager));
		return CommonUtil.getPageJsonStr(pager);
	
		
	}
	
	
	//向用户展示接口的信息用下拉列表
	
		@RequestMapping(value = "/doUserServiceList",method = {RequestMethod.GET,RequestMethod.POST})
		@ResponseBody
		public ModelAndView doUserServiceList(HttpServletRequest request,String userlogname) {

			ModelAndView mav = new ModelAndView("/framework/interfaceservice/touser/showservice");

			String showservicetouser=request.getParameter("userlogname");
			//根据用户登录名找出用户id
			String userid=service.queryUserIdByLogName(userlogname);
			SessionManager.set("userlognameshowservice", userlogname);
			
			DataUserModel dm=new DataUserModel();
			dm.setUserid(userid);
		//	DataUserModel user=(DataUserModel) service.findUserById(dm).get(0);
			DataUserModel user=(DataUserModel) service.findUserByIdModel(dm);
			String valid1=user.getValidity();
			String para=user.getParameter();
			if(valid1.equals("0")){
				ModelAndView mav1 = new ModelAndView("/framework/interfaceservice/touser/notify");

				mav1.addObject("msg","用户无效");
				return mav1;
			}
			
			SessionManager.set("useridshowservice", userid);
		//	String li=service.queryList(userid);
			Pager pager=service.queryListA(userid);
			List listtemp=service.queryListB(userid);
			List<DataUserServiceModel> li=new ArrayList<DataUserServiceModel>();
			for(int i=0;i<listtemp.size();i++){
				DataUserServiceModel temp=(DataUserServiceModel) listtemp.get(i);
				String codesrvname=CodeTableManager.getNameByCode("interface_srv_type", temp.getSrvtype());
				temp.setSrvtype(codesrvname);
				String valid=temp.getValidity();
				if(valid.equals("1")){
					li.add(temp);
				}
			}
			
			/*for(int i=0;i<pager.getResultList().size();i++){
				DataUserServiceModel temp=(DataUserServiceModel) pager.getResultList().get(i);
				String codesrvname=CodeTableManager.getNameByCode("interface_srv_type", temp.getSrvtype());
				temp.setSrvtype(codesrvname);
				String valid=temp.getValidity();
				if(valid.equals("1")){
					li.add(temp);
				}
			}*/
			mav.addObject("list", li);
			mav.addObject("userid",userid);
			mav.addObject("para",para);
			return mav;
		
			
		}
	
	
	
	
	
	//向用户展示接口详细信息中转页面
	
	@RequestMapping(value = "/gotoUserServiceDetail",  method = {RequestMethod.GET,RequestMethod.POST})
	public String gotoUserServiceDetail(HttpServletRequest request) {
		String srvid=request.getParameter("srvid");
		SessionManager.set("showservicedetailtouser", srvid);
		return "/framework/interfaceservice/touser/servicedetailtouser";
	}
	
	
	
	//向用户展示接口详细的信息
	
	@RequestMapping(value = "/doUserServiceDetail", method = { RequestMethod.GET, RequestMethod.POST },produces = "application/json; charset=utf-8")
	@ResponseBody
	public ModelAndView doUserServiceDetail(HttpServletRequest request,String srvid) {
		//String srvId = SessionManager.getString("showservicedetailtouser");
		String userid=SessionManager.getString("useridshowservice");
		String userlogname=SessionManager.getString("userlognameshowservice");
		//String srvid=SessionManager.getString("useridshowservice");
		String para=request.getParameter("para");
		
		DataServiceModel ds=new DataServiceModel();
		ds.setSrvid(srvid);
		
		DataServiceModel dstemp=service.getDataServiceModel(ds);
		String srvtype=dstemp.getSrvtype();
		
		
		String fin = service.findSqlDesc(srvid,para);
		
		
		JSONObject finjson = JSONObject.fromObject(fin);
		//JSONArray array=(JSONArray)finjson.get("servicecondition");
		ModelAndView mav;
		if(srvtype.equals("01")){
			mav = new ModelAndView("/framework/interfaceservice/touser/servicedetailtouser");

		}else if(srvtype.equals("03")){
			mav = new ModelAndView("/framework/interfaceservice/touser/insertdatatouser");
		}else{
			mav = new ModelAndView("/framework/interfaceservice/touser/updatedatatouser");
		}
		
		mav.addObject("finjson", finjson);
		mav.addObject("userid", userid);
		mav.addObject("srvid", srvid);
		mav.addObject("srvtype", srvtype);
		mav.addObject("userlogname", userlogname);
		mav.addObject("para", para);
		return mav;
	}
	
	
	
	
	//向用户展示接口详细的信息在一个页面上
	
	@RequestMapping(value = "/doUserServiceDetailOneJsp", method = { RequestMethod.GET, RequestMethod.POST },produces="text/html;charset=UTF-8")
	@ResponseBody
	public String doUserServiceDetailOneJsp(HttpServletRequest request,String srvid) {
		//String srvId = SessionManager.getString("showservicedetailtouser");
		String userid=SessionManager.getString("useridshowservice");
		//String srvid=SessionManager.getString("useridshowservice");
		String para=request.getParameter("para");
		
		String fin = service.findSqlDesc(srvid,para);
		
		
		JSONObject finjson = JSONObject.fromObject(fin);
		//JSONArray array=(JSONArray)finjson.get("servicecondition");
		JSONObject obj1=new JSONObject();
		obj1.put("finjson", finjson);
		obj1.put("userid", userid);
		obj1.put("srvid", srvid);
		
		
		//拼接标签
		JSONObject ser=(JSONObject)finjson.get("service");
		String servicename=(String)ser.get("servicename");
		String interfacedesc=(String)ser.get("interfacedesc");

		String str="<tr> <th width='15%'>服务名称"
				+":</th>"
				+"<td width='30%'>"+servicename
				+"</td></tr>".replaceAll("\r","").replaceAll("\n","");
		str+="<tr> <th width='15%'>接口信息描述"
				+":</th>"
				+"<td width='30%'>"+interfacedesc
				+"</td></tr>".replaceAll("\r","").replaceAll("\n","");	
		
		
		
		JSONArray array=(JSONArray)finjson.get("servicecondition");
		for(int i=0;i<array.size();i++){
			JSONObject obj=(JSONObject)array.get(i);
			String required=(String)obj.get("required");
			String name=(String)obj.get("parametername");
			String paratype=(String)obj.get("paratype");
			String parameter_means=(String)obj.get("parameter_means");
			
			if(required.equals("否")){
			str+="<tr> <th width='15%'>"+name
					+":</th>"
					+"<td width='30%'><input class='easyui-textbox' type='text' width='20%' align='center'  name='"+name
					+"'"+"/></td> <th width='15%'>参数类型："+paratype
					+"</th><th width='15%'>参数含义："+parameter_means
					+"</th></tr>".replaceAll("\r","").replaceAll("\n","");
				}else if(required.equals("是")){
						str+="<tr> <th width='15%'>"+name
						+":</th>"
						+"<td width='30%'><input data-options='required:true' class='easyui-textbox' type='text' width='20%' align='center'  name='"+name
						+"'"+"/></td><th width='15%'>参数类型："+paratype
						+"</th>"
						+"<th width='15%'>参数含义："+parameter_means
						+"</th></tr>".replaceAll("\r","").replaceAll("\n","");
			
		}
		
		}
		return str;
	}
}
